/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             MessageService.svc.cs
*        创建系统时间:       2012/11/2 19:07:05
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class MessageService : MarshalByRefObject, IMessageService
    {
        private static MessageService instance = null;
        public static DateTime LaseCheck = DateTime.MinValue;
        private static object locker = new object();
        public static bool MessageSend = true;

        public int BatchSend(BaseUserInfo userInfo, string[] receiverIDs, string[] organizeIDs, string[] roleIDs, string content)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseMessageDao dao = new BaseMessageDao(dbHelper, userInfo);
                BaseMessageEntity messageEntity = new BaseMessageEntity
                {
                    Content = content
                };
                num = dao.BatchSend(receiverIDs, organizeIDs, roleIDs, messageEntity, true);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable GetInnerOrganize(BaseUserInfo userInfo)
        {
            DataTable innerOrganize = new DataTable(BaseOrganizeTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                innerOrganize = new BaseOrganizeDao(dbHelper, userInfo).GetInnerOrganize(string.Empty);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return innerOrganize;
        }

        public DataTable GetNewList(BaseUserInfo userInfo)
        {
            DataTable newDT = new DataTable();
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                newDT = new BaseMessageDao(dbHelper, userInfo).GetNewDT();
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return newDT;
        }

        public void Load()
        {
        }

        public string[] MessageChek(BaseUserInfo userInfo, string lastChekDate)
        {
            string[] strArray = null;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                new BaseUserDao(dbHelper, userInfo).OnLine(userInfo.ID);
                strArray = new BaseMessageDao(dbHelper, userInfo).MessageChek();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return strArray;
        }

        public void Read(BaseUserInfo userInfo, string id)
        {
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                new BaseMessageDao(dbHelper, userInfo).Read(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
        }

        public DataTable ReadFromReceiver(BaseUserInfo userInfo, string receiverID)
        {
            DataTable table = new DataTable();
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseMessageDao(dbHelper, userInfo).ReadFromReceiver(receiverID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public string Send(BaseUserInfo userInfo, string receiverID, string content)
        {
            string str = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseMessageEntity messageEntity = new BaseMessageEntity
                {
                    ID = Guid.NewGuid().ToString(),
                    CategoryID = MessageCategory.Send.ToString(),
                    FunctionID = MessageFunction.Message.ToString(),
                    ReceiverID = receiverID,
                    Content = content
                };
                str = new BaseMessageDao(dbHelper, userInfo).Add(messageEntity);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public static MessageService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new MessageService();
                        }
                    }
                }
                return instance;
            }
        }
    }
}